// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: declaration of CImplRepeat.
//   $Id$

#pragma once
#include "nodesetBinding.h"


class CXFormsElementRepeat;
class CImplGroup;

class CImplRepeat : public CImplNodesetBinding
{
	friend class CXFormsParseRepeat;
public:
	CImplRepeat(CXFormsElementRepeat* pElement);
	virtual ~CImplRepeat();

	//CImpl
	virtual HRESULT init(CImpl * pParent);

#if 0
	virtual HRESULT GetContextNode(CImplModel** ppModel, CNode** ppContextNode, ULONG ulOffset);
#endif
	HRESULT onscrollFirst();
	HRESULT onscrollLast();
	HRESULT setCursor(BSTR sCursor);
	HRESULT setCursor(unsigned long ulCursor);

public:
	virtual void buildDependencies();
	HRESULT setCursor(IXMLDOMNode *pNode);
	virtual HRESULT doInitialize();
	virtual CIdResolver * GetIdResolver() const;
	virtual HRESULT refresh();
	virtual HRESULT AddChild(CImpl * pChild);
	virtual HRESULT RemoveChild(CImpl * pChild);

	inline ULONG get_Iterations()
	{
		//read-only access to the requested iteration count.
		return m_ulNumber;
	}
	virtual HRESULT oninvalid(){return S_OK;};
	virtual HRESULT onvalid(){return S_OK;};
	virtual HRESULT onreadonly(){return S_OK;};
	virtual HRESULT onreadwrite(){return S_OK;};
	virtual HRESULT onrequired(){return S_OK;};
	virtual HRESULT onoptional(){return S_OK;};
	virtual HRESULT onenabled(){return S_OK;};
	virtual HRESULT ondisabled(){return S_OK;};
	virtual ULONG getCursor();
	HRESULT decrementIndex();
	HRESULT incrementIndex();
private:
	ULONG m_ulNumber;				//number of repetitions.
	//This is probably m_ulScrollOffset in NodeSetBinding
	ULONG m_ulStartIndex;			//current first displayed repetition
	ULONG m_ulInitialStartIndex;	//for resetting the form.

	bool m_bCursorRequested;

	bool m_bIsResizeable;
	inline HRESULT refreshIterationBindings();
	CArray<CImplGroup *,CImplGroup *> m_Iterations;
//	IUIRepeatEvents * m_pHostEventHandler;
public:
	// passes the focus through to the current repeat-item
	virtual HRESULT showFocus(void);
private:
	// <summary>checks whether to rebuild the MDT when the index changes</summary>
	bool rebuildOnIndexChange(void);
public:
	/// <summary>This is called from CIdResolver::remove() to check with the owner of the CIdResolver 
	/// will allow the id to be removed. The only sitution where this method will return false 
	/// is when the owner is a xf:repeat with iterations. See Bug 954 for details.</summary>
	virtual inline bool CanRemoveIdFromResolver(void) const;
};

inline bool CImplRepeat::CanRemoveIdFromResolver(void) const {
	bool bCanRemove = false;
	// If the group is a repeat iterations then don't remove the id
	if(!m_Iterations.GetSize())
		bCanRemove = true;
	return bCanRemove;
}